package com.system.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.system.demo.entity.DTO.NoticeSearchDTO;
import com.system.demo.service.IssueNoticeService;
import com.system.demo.tool.ResponseResult;
import com.system.demo.utils.MinioUtils;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/Issue")
public class NoticeController {

    @Autowired
    IssueNoticeService issueNoticeService;

    @Autowired
    MinioClient minioClient;

    @Autowired
    MinioUtils minioUtils;

    /**
     * 管理发布通知
     * @param file 通知文件
     * @param title 通知标题
     * @param files 附件
     * @return
     */
    @PostMapping("/IssueNotice")
    @SaCheckPermission("SchoApprove")
    public ResponseResult<?> issueNotice(@RequestParam String title, @RequestParam String deadLine, @RequestParam("file") MultipartFile file, @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        // 解析 deadLine 字符串为 LocalDateTime
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime deadline = LocalDateTime.parse(deadLine, formatter);

        List<MultipartFile> safeFiles = files == null ? Collections.emptyList() : files;
        return issueNoticeService.IssueNotice(title, deadline, file, safeFiles);
    }

    /**
     *管理批量删除通知
     * @param TableIds 通知表ids
     * @return
     */
    @PostMapping("/DeleteNotice")
    @SaCheckPermission("SchoApprove")
    public ResponseResult<?> deleteNotice(@RequestParam List<Integer> TableIds) {
        return issueNoticeService.deleteNotice(TableIds);
    }


    /**
     * 下载通知附件
     * @param TableId 学生查看的通知id
     * @return 返回通知素有的附件
     */
    @PostMapping("/DownloadAttachment")
    @SaCheckLogin
    public void downloadAttachment(@RequestParam Integer TableId, HttpServletResponse response) {
        issueNoticeService.DownloadAttachment(TableId,response);
    }

    /**
     * 通知对应的附件记录
     * @param TableId
     * @return
     */
    @PostMapping("/GetAttachments")

    public ResponseResult<?> getAttachments(@RequestParam Integer TableId) {
        return issueNoticeService.GetAttachments(TableId);
    }


    /**
     * 学生上传评奖申请（带文件）
     * @param file 学生 提交附件
     * @param NoticeId 对应的通知
     * @return
     */
    // 未审核 -> 已审核 / 已驳回 -> 再次提交
    @PostMapping("/SubmitApplication")
    public ResponseResult<?> submitApplication(@RequestParam("file") MultipartFile file, @RequestParam Integer NoticeId) {
        return issueNoticeService.SumbitApplication(file,NoticeId);
    }


    /**
     * 按筛选条件返回对应的申请记录
     * @param noticeSearchDTO
     * @return
     */
    @PostMapping("/SearchApplication")
    public ResponseResult<?> searchApplication(@RequestBody NoticeSearchDTO noticeSearchDTO, @RequestParam Integer current,@RequestParam Integer size) {
        return issueNoticeService.SearchApplication(noticeSearchDTO,current,size);
    }

    /**
     * 管理导出用户提交的附件
     * @param ApplicationIds 申请表的ids
     * @param response 直接发请求
     */
    // todo 返回的是姓名而不是学号
    @PostMapping("/ExportAttachment")
    public void exportAttachment(@RequestParam List<Integer> ApplicationIds,HttpServletResponse response) {
        issueNoticeService.ExportAttachment(response, ApplicationIds);
    }
    //教师按条件筛选申请表(年级 + 班级)


    //教师审核申请（已完成审核，等待通知）

    //post方法写一个前端进入网站时候的触发，将viewcount + 1 ,要对应的通知id

    //写一个前端开始下载文件时候的post方法，让download count + 1， 要对应下载文件的 id

    /**
     * 前端获取通知列表
     * @param current
     * @param size
     * @return
//     */
    @PostMapping("/GetNotice")
    public ResponseResult<?> getNotice(@RequestParam Integer current,@RequestParam Integer size) {
        return issueNoticeService.GetNotice(current,size);
    }

    /**
     * 按照申请表通知获取整个记录
     * @param ApplicationId
     * @return
     */
    @PostMapping("/GetApplicationAttachment")
    public ResponseResult<?> getApplicationAttachment(@RequestParam Integer ApplicationId) {
        return issueNoticeService.GetApplicationAttachment(ApplicationId);
    }

    /**
     * 管理员审核学生的公告申请
     * @param ApplicationId
     * @param isPass
     * @param reason
     * @param file
     * @return
     */
    @PostMapping("/Determine")
    public ResponseResult<?> determine(@RequestParam Integer ApplicationId, @RequestParam String isPass, @RequestParam(required = false) String reason, @RequestParam(value = "file" ,required = false) MultipartFile file) {
        return issueNoticeService.Determine(ApplicationId, isPass, reason, file);
    }

    /**
     *
     * @param current
     * @param size
     * @param noticeSearchDTO
     * @return
     */
    @PostMapping("/GetSelfApplication")
    public ResponseResult<?> getSelfApplication(@RequestParam Integer current, @RequestParam Integer size, @RequestBody NoticeSearchDTO noticeSearchDTO) {
        return issueNoticeService.GetSelfApplication(current, size, noticeSearchDTO);
    }
}
